<template>
  <div class="nav-menu">
    <div class="logo">
      <img class="img" src="@/assets/img/logo.svg" alt="logo" />
      <span v-show="!isFold" class="title">弘源管理系统</span>
    </div>
    <el-menu
      :collapse="isFold"
      :default-active="activeDefault"
      text-color="#b7bdc3"
      active-text-color="#fff"
      background-color="#001529"
    >
      <template v-for="item in userMenus" :key="item.id">
        <!-- 1.系统总览 -->
        <el-sub-menu :index="item.id + ''">
          <template #title>
            <!-- <el-icon><Monitor /></el-icon> -->
            <!-- 使用动态组件渲染图标 -->
            <el-icon><component :is="item.icon.split('el-icon-')[1]"></component></el-icon>
            <span>{{ item.name }}</span>
          </template>
          <template v-for="subitem in item.children" :key="subitem.id">
            <el-menu-item :index="subitem.id + ''" @click="handelItemClick(subitem)">{{ subitem.name }}</el-menu-item>
          </template>
        </el-sub-menu>
      </template>
    </el-menu>
  </div>
</template>

<script setup lang="ts" name="nav-menu">
import { ref } from 'vue'
import useLoginStore from '@/store/login'
import { useRouter } from 'vue-router'
// 0.定义数据
defineProps({
  isFold: {
    type: Boolean,
    default: false
  }
})
// 1.获取菜单数据
const loginStore = useLoginStore()
const userMenus = loginStore.userMenus
const router = useRouter()
//默认菜单(默认激活第一项)
console.log(userMenus[0])

const activeDefault = ref(userMenus[0].children[0].id + '')
function handelItemClick(item: any) {
  const url = item.url
  router.push(url)
}
</script>

<style scoped lang="less">
.nav-menu {
  height: 100%;
  background-color: #001529;
}

.logo {
  display: flex;
  height: 28px;
  padding: 12px 10px 8px 10px;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  overflow: hidden;

  .img {
    height: 100%;
    margin: 0 10px;
  }

  .title {
    font-size: 16px;
    font-weight: 700;
    color: white;
    white-space: nowrap;
  }
}

/**
  解决左侧菜单不对齐问题
*/
.el-menu {
  border-right: none;
  user-select: none;
}

.el-sub-menu {
  .el-menu-item {
    padding-left: 50px !important;
    background-color: #0c2135;
  }

  .el-menu-item:hover {
    color: #fff;
  }

  .el-menu-item.is-active {
    background-color: #0a60bd;
  }
}
</style>
